﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Celeriq.AdminSite.Objects;

namespace Celeriq.AdminSite.AuthUser
{
    public partial class Default : Celeriq.AdminSite.Objects.BasePage
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            cmdSearch.Click += cmdSearch_Click;
            cmdClear.Click += cmdClear_Click;
            grdItem.RowDataBound += grdItem_RowDataBound;
            grdItem.Sorting += grdItem_Sorting;

            var url = new PagingURL(this.Request.Url.PathAndQuery);
            txtKeyword.Text = url.Text;
        }

        private void grdItem_Sorting(object sender, GridViewSortEventArgs e)
        {
            var url = new PagingURL(this.Request.Url.PathAndQuery);
            url.SortExpression = e.SortExpression;
            url.SortDirection = (e.SortDirection == SortDirection.Ascending ? SortConstants.Asc : SortConstants.Desc);
            url.ResetPaging();
            this.Response.Redirect(url.ToString());
        }

        private void cmdClear_Click(object sender, EventArgs e)
        {
            var url = new PagingURL(this.Request.Url.PathAndQuery);
            url.Text = string.Empty;
            url.ResetPaging();
            this.Response.Redirect(url.ToString());
        }

        private void cmdSearch_Click(object sender, EventArgs e)
        {
            var url = new PagingURL(this.Request.Url.PathAndQuery);
            url.Text = txtKeyword.Text;
            url.ResetPaging();
            this.Response.Redirect(url.ToString());
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            this.Populate();
        }

        private void Populate()
        {
            var url = new PagingURL(this.Request.Url.PathAndQuery);

            int count;
            grdItem.DataSource = RepositoryConnection.GetRepositoryPropertyList(new Common.PagingInfo
            {
                PageOffset = PagingControl1.PageIndex,
                RecordsPerPage = PagingControl1.RecordsPerPage,
                Keyword = txtKeyword.Text,
                SortField = url.SortExpression,
                SortAsc = (url.SortDirection == SortConstants.Asc),
            }, out count);
            grdItem.DataBind();
            PagingControl1.ItemCount = count;
        }

        private void grdItem_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var dataItem = e.Row.DataItem as Client.Interfaces.BaseRemotingObject;

                var lblID = e.Row.FindControl("lblID") as Literal;
                var lblName = e.Row.FindControl("lblName") as Literal;
                var lblDisk = e.Row.FindControl("lblDisk") as Literal;
                var lblMemory = e.Row.FindControl("lblMemory") as Literal;
                var lblHash = e.Row.FindControl("lblHash") as Literal;
                var lblCount = e.Row.FindControl("lblCount") as Literal;
                var lblCreated = e.Row.FindControl("lblCreated") as Literal;

                lblID.Text = dataItem.Repository.ID.ToString();
                lblName.Text = dataItem.Repository.Name;
                lblDisk.Text = Celeriq.AdminSite.Objects.Utilities.ToSizeDisplay(dataItem.DataDiskSize);
                lblMemory.Text = Celeriq.AdminSite.Objects.Utilities.ToSizeDisplay(dataItem.DataMemorySize);
                lblHash.Text = dataItem.Repository.VersionHash.ToString();
                lblCount.Text = dataItem.ItemCount.FormatNumber();
                lblCreated.Text = dataItem.Repository.CreatedDate.ToString("yyyy-MM-dd HH:mm:ss");
            }
        }

    }
}